package com.ygswglxt.leave.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ygswglxt.leave.entity.LeaveBalance;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface LeaveBalanceMapper extends BaseMapper<LeaveBalance> {
    
    /**
     * 获取所有员工的假期余额（联表查询员工名称）
     */
    @Select("SELECT lb.*, COALESCE(u.name, u.username) as userName " +
            "FROM leave_balance lb " +
            "LEFT JOIN sys_user u ON lb.user_id = u.id " +
            "ORDER BY lb.update_time DESC")
    List<LeaveBalance> selectAllWithUserName();
    
    /**
     * 根据用户ID获取假期余额（联表查询员工名称）
     */
    @Select("SELECT lb.*, COALESCE(u.name, u.username) as userName " +
            "FROM leave_balance lb " +
            "LEFT JOIN sys_user u ON lb.user_id = u.id " +
            "WHERE lb.user_id = #{userId}")
    LeaveBalance selectByUserIdWithUserName(Long userId);
}

